const {findStore, bestFindClose, findBuilding, findRepair, findFlagList} = require("./Find");
const {MyRoomList} = require("./Param");
module.exports.initDefault = function () {
    if (Memory.default || Game.time % 5000 === 0) {
        for (let i = 0; i < MyRoomList.length; i++) {//遍历我设定的房间
            let room = Game.rooms[MyRoomList[i]]
            if(room.find(FIND_MY_SPAWNS).filter(spawn=>{spawn.spawning}).length===0){
                room.memory.harvestList = [];
                room.memory.transList = [];
                room.memory.butlerList = [];
                room.memory.repairList = [];
                room.memory.builderList = [];
                room.memory.upList = [];
                room.memory.defenseList = [];
                room.memory.outHarvestList = [];
                room.memory.reserveList = [];
                room.memory.guardList = [];
            }
        }
        Memory.default = false;
    }
    Creep.prototype.usedStore = function (sourceType = RESOURCE_ENERGY) {
        return this.store.getUsedCapacity(sourceType);
    };

    Creep.prototype.freeStore = function (sourceType = RESOURCE_ENERGY) {
        return this.store.getFreeCapacity(sourceType);
    };

    Creep.prototype.maxStore = function (sourceType = RESOURCE_ENERGY) {
        return this.store.getCapacity(sourceType);
    };

    Creep.prototype.initFull = function () {
        if (this.memory.isFull === undefined || this.memory.isFull === null) {
            this.memory.isFull = false;
            return;
        }
        if (this.freeStore() === 0) {//已满
            this.memory.isFull = true;
            return;
        }
        if (this.usedStore() === 0) {//背包已空
            this.memory.isFull = false;
        }
    }

    Creep.prototype.bestMove = function (to) {
        let result = this.moveTo(to, {
            reusePath: 20,
            visualizePathStyle: {
                fill: 'transparent',
                stroke: '#fff',
                lineStyle: 'dashed',
                strokeWidth: .15,
                opacity: .1
            }
        });
        if (result !== 0 && result !== -11 && result !== -4) {
            // 如果路径被阻挡，creep.moveTo 会尝试重新计算路径并移动
            result = this.moveTo(to, {
                reusePath: 5,
                visualizePathStyle: {
                    fill: 'transparent',
                    stroke: 'red',
                    lineStyle: 'dashed',
                    strokeWidth: .15,
                    opacity: .1
                }
            });
        }
        return result;
    }

    Creep.prototype.bestOutMove = function (to) {
        let result = this.moveTo(to, {
            reusePath: 30,
            visualizePathStyle: {
                fill: 'transparent',
                stroke: '#fff',
                lineStyle: 'dashed',
                strokeWidth: .15,
                opacity: .1
            }
        });
        if (result !== 0 && result !== -11 && result !== -4) {
            // 如果路径被阻挡，creep.moveTo 会尝试重新计算路径并移动
            result = this.moveTo(to, {
                reusePath: 10,
                ignoreCreeps: true,
                visualizePathStyle: {
                    fill: 'transparent',
                    stroke: 'red',
                    lineStyle: 'dashed',
                    strokeWidth: .15,
                    opacity: .1
                }
            });
        }
        return result;
    }
    Creep.prototype.bestAttack = function (to) {
        let status = this.attack(to)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        } else {
            this.memory.closeId = undefined;
        }
    }
    Creep.prototype.bestAttackController = function (to) {
        let status = this.attackController(to)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        }
    }
    Creep.prototype.bestBuild = function (to) {
        let status = this.build(to)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        } else {
            this.memory.closeId = undefined;
        }
    }
    Creep.prototype.bestClaimController = function (to) {
        let status = this.claimController(to)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        }
    }
    Creep.prototype.bestDismantle = function (to) {
        let status = this.dismantle(to)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        }
        return status
    }
    Creep.prototype.bestHarvest = function (to) {
        let status = this.harvest(to)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        }
    }
    Creep.prototype.bestHeal = function (to) {
        let status = this.heal(to)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        }
    }
    Creep.prototype.bestPickup = function (to) {
        let status = this.pickup(to)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        } else {
            this.memory.closeId = undefined;
        }
    }
    Creep.prototype.bestPull = function (to) {
        let status = this.pull(to)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        }
    }
    Creep.prototype.bestRangedAttack = function (to) {
        let status = this.rangedAttack(to)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        }
    }
    Creep.prototype.bestRangedHeal = function (to) {
        let status = this.rangedHeal(to)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        }
    }
    Creep.prototype.bestRangedMassAttack = function (to) {
        let status = this.rangedMassAttack()
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        }
    }
    Creep.prototype.bestRepair = function (to) {
        let status = this.repair(to)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        } else {
            this.memory.closeId = undefined;
        }
    }
    Creep.prototype.bestReserveController = function (to) {
        let status = this.reserveController(to)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        }
    }
    Creep.prototype.bestSignController = function (to, sign) {
        let status = this.signController(to, sign)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        }
    }
    Creep.prototype.bestTrans = function (to, resourceType = RESOURCE_ENERGY) {
        let status = this.transfer(to, resourceType)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        } else {
            this.memory.closeId = undefined;
        }
        return status;
    }
    Creep.prototype.bestUpController = function (to) {
        let status = this.upgradeController(to)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        }
    }
    Creep.prototype.bestWithdraw = function (to, resourceType = RESOURCE_ENERGY) {
        let status = this.withdraw(to, resourceType)
        if (status === ERR_NOT_IN_RANGE) {
            this.bestMove(to);
        } else {
            this.memory.closeId = undefined;
            this.memory.bindId = undefined;
        }
        return status;
    }

    Creep.prototype.bestGetSource = function () {
        let fullStore = findStore(this.room, 1);
        if (this.room.controller.level < 2 && !this.room.memory.mustSpawning && fullStore.length === 0) {
            let extensionList = this.room.find(FIND_MY_STRUCTURES, {filter: site => (site.structureType === STRUCTURE_EXTENSION || site.structureType === STRUCTURE_SPAWN) && site.usedStore() > 0})
            fullStore = fullStore.concat(extensionList);
        }
        if (fullStore.length === 0 && this.room.terminal && this.room.terminal.usedStore() > 1000) {
            fullStore = fullStore.concat(this.room.terminal);
        }
        let transStore = this.findBindClose(fullStore);
        if (transStore) {
            this.say("🔋")
            this.bestWithdraw(transStore);
        } else {
            this.memory.closeId = undefined;
        }
    }

    Creep.prototype.findBindClose = function (list) {
        let transStore;
        if (this.memory.closeId === undefined) {
            transStore = bestFindClose(this, list)
            if (transStore) {
                this.memory.closeId = transStore.id
            }
            return transStore;
        } else {
            return Game.getObjectById(this.memory.closeId);
        }
    }
    Creep.prototype.building = function () {
        let containerToBuild = findBuilding(this.room, "site");
        let transStore = this.findBindClose(containerToBuild);
        this.bestBuild(transStore)
    }
    Creep.prototype.repairing = function (list = undefined) {
        if (list === undefined) {
            list = findRepair(this.room);
        }
        // 选择需要修复的建筑中生命值最低的一个
        list.sort((a, b) => a.hits - b.hits);
        // 开始修复建筑
        this.bestRepair(list[0]);
    }

    Creep.prototype.moveToStop = function () {
        let stopList = findFlagList("stop").filter(flag => flag.room && flag.room.name === this.room.name);
        if (stopList.length > 0) {
            let stopPos = stopList[0];
            if (!this.pos.isNearTo(stopPos)) {
                this.say("🅿️");
                this.bestMove(stopPos);
            }
        }

    }

    Structure.prototype.usedStore = function (sourceType = RESOURCE_ENERGY) {
        return this.store.getUsedCapacity(sourceType);
    };

    Structure.prototype.freeStore = function (sourceType = RESOURCE_ENERGY) {
        return this.store.getFreeCapacity(sourceType);
    };

    Structure.prototype.maxStore = function (sourceType = RESOURCE_ENERGY) {
        return this.store.getCapacity(sourceType);
    };
}
